package cn.gs.controller.streamTest;

import java.util.*;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class StreamTest {

    public static void main(String[] args) {
        //传统操作List的方法
        traditionListMethod();
        //stream流操作list
        listStreamMethod ();
        //集合Collection (list 、set) 、map 、数组，获取Stream流
        getListSetMapArrayStream();
        // Stream 流常用api
        streamApiMethod();
    }

    /**
     * 传统操作List的方法
     */
    public static void traditionListMethod() {
        List<String> list = new ArrayList<String>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("马六");
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");
        //获取姓张的名字
        List<String> nameList = new ArrayList<String>();
        for(String  str : list){
            if(str.startsWith("张")){
                nameList.add(str);
            }
        }
        //获取姓张名字，长度为3的姓名
        List<String> lengthList = new ArrayList<String>();
        for( String str : nameList){
            if( str.length() >=3 ){
                lengthList.add(str);
            }
        }
        //遍历list
        for(int i=0; i< lengthList.size() ; i++ ){
            System.out.println( "姓名:" + lengthList.get(i) );
        }
        /*for( String str : lengthList){
            System.out.println("姓名:" + str);
        }*/
    }

    /**
     * stream流操作list
     */
    public static void listStreamMethod (){
        List<String> list = new ArrayList<String>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("马六");
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");

        list.stream().filter( str -> str.startsWith("张"))
                     .filter(len -> len.length()>=3 )
                     .forEach( res -> System.out.println("stream操作list : " + res));
    }

    //集合Collection (list 、set) 、map 、数组，获取Stream流
    public static void getListSetMapArrayStream(){
        // 获取 list 的Stream流
        List<String> list = new ArrayList<String>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("马六");
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");
        Stream<String> listStream = list.stream();
        System.out.println("获取 list 的Stream流: "+ listStream);
        // 获取 set 的Stream流
        Set<String> set = new HashSet<String>();
        set.add("巩俐");
        set.add("王祖贤");
        set.add("刘亦菲");
        Stream<String> setStream = set.stream();
        System.out.println("获取 set 的Stream流: "+ setStream);
        // 获取 map 的Stream流
        Map <String ,String > map = new HashMap<String ,String >();
        map.put("赵丽颖", "28");
        map.put("孙俪", "45");
        map.put("刘晓庆", "55");
        Stream <String> keySetStream =map.keySet().stream();
        System.out.println("获取 map 的Stream流: "+ keySetStream);
        Stream <String> valuesStream = map.values().stream();
        System.out.println("获取 map 的Stream流: "+ valuesStream);
        Stream < Map.Entry<String ,String > > entryStream= map.entrySet().stream();
        System.out.println("获取 map 的Stream流: "+ entryStream);
        // 获取 array 的Stream流
        Stream  <String> arrayStream = Stream.of("柳岩" , "杨丽萍" ,"李思思");
        System.out.println("获取 array 的Stream流: "+ arrayStream);

    }

    /**
     * stream流常用API
     */
    public static void streamApiMethod (){
        List<String> list = new ArrayList<String>();
        list.add("张三");
        list.add("李四");
        list.add("王五");
        list.add("马六");
        list.add("张三丰");
        list.add("张无忌");
        list.add("张翠山");

        list.stream().filter( str -> str.startsWith("张"))
                .filter(len -> len.length()>=3 )
                .forEach( res -> System.out.println("stream操作list : " + res));

        list.stream().limit(4).forEach( res -> System.out.println("限制输出前4条: " + res));
        System.out.println("计数: " +  list.stream().count() );
        list.stream().skip(3).forEach( sk -> System.out.println("跳过前3条: " + sk ) );

        //map中value类型由String转化为int
        Stream  <String> arrayStream = Stream.of("18" , "28" ,"20");
        Stream <Integer> entryStream =  arrayStream.map(m -> Integer.parseInt(m));
        entryStream.forEach( s -> System.out.println("由String转化为int: "+ s));

        //合并两个Stream流
        Stream <String> streamA = Stream.of ("张小泉") ;
        Stream <String> streamB = Stream.of ("王麻子") ;
        Stream <String> streamAB = Stream.concat(streamA , streamB) ;
        streamAB.forEach( res -> System.out.println("合并两个Stream流: "+ res));

        //collectors 搜集器操作 list 的Stream 流
        Stream <String> listStream= list.stream().filter( li -> li.length()==3);
        List <String> newList= listStream.collect(Collectors.toList());
        for(String str : newList){
            System.out.println("从 list 集合中搜集出新的 list 集合: " + str );
        }

        //collectors 搜集器操作 set 的Stream 流
        Set<String> set = new HashSet<String>();
        set.add("巩俐");
        set.add("王祖贤");
        set.add("刘亦菲");
        Stream<String> setStream = set.stream().filter( s -> s.length() >=3);
        Set<String> newSet = setStream.collect(Collectors.toSet());
        for(String str : newSet){
            System.out.println("从 set 集合中搜集出新的 set 集合: " + str );
        }

        //collectors 搜集器操作 map 的Stream 流
        String [] nameArray = { "林青霞,28" , "张曼玉,38" , "巩俐,48" , "柳岩,33"};
        Stream <String> arrayStream01 = Stream.of(nameArray).filter(str -> Integer.parseInt( str.split(",")[1] ) >28 );
        Map <String ,Integer> map = arrayStream01.collect(
                                    Collectors.toMap( s -> s.split(",") [0] ,
                                                      s -> Integer.parseInt(s.split(",")[1])) );
        for( Map.Entry<String ,Integer>   entry: map.entrySet()){
            System.out.println("collectors 搜集器操作 map 的Stream 流: ");
            System.out.println(entry.getKey());
            System.out.println(entry.getValue());
        }
    }
}
